package com.erp.auto.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.erp.auto.domain.*;
import com.erp.auto.mapper.*;
import com.erp.auto.service.ErpQueryProductInfoService;
import com.ruoyi.common.core.domain.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class ErpQueryProductInfoServiceImpl implements ErpQueryProductInfoService {
    @Resource
    ErpAutoPulishProductProjectMapper erpAutoPulishProductProjectMapper;
    @Resource
    ErpAutoUnshelfProductConfigMapper erpAutoUnshelfProductConfigMapper;
    @Resource
    ErpAutoEvaluationConfigMapper erpAutoEvaluationConfigMapper;
    @Resource
    ErpAutoPriceOffProductProjectMapper erpAutoPriceOffProductProjectMapper;
    @Resource
    ErpAutoPriceRiseProductProjectMapper erpAutoPriceRiseProductProjectMapper;
    @Resource
    ErpAutoInfoModuleConfigMapper erpAutoInfoModuleConfigMapper;
    @Resource
    ErpAutoSpeciesCouponConfigMapper erpAutoSpeciesCouponConfigMapper;
    @Resource
    ErpAutoGoldCoinConfigMapper erpAutoGoldCoinConfigMapper;
    @Resource
    ErpAutoEvaluationConfigSingleMapper erpAutoEvaluationConfigSingleMapper;
    @Resource
    ErpAutoEvaluationConfigTaskMapper erpAutoEvaluationConfigTaskMapper;
    @Override
    public R<Object> getAllAutoProjectByStoreId(Long storeId, String projectName, String type, Integer status) {
        if (Objects.isNull(type)) {
            type = "0,1,2,3,4,5,6,7,51,52";
        }

        List<HashMap<String, Object>> mapList = new ArrayList<>();

        Map<String, String> myMap = new HashMap<>();
        String[] pairs = type.split(",");
        for (String pair : pairs) {
            // 使用等号分割键和值
            myMap.put(pair, pair); // 去除空格
        }
        //查询上架的list
        if (ObjectUtils.isNotNull(myMap.get("0"))) {
            //上架列表
            List<ErpAutoPulishProductProject> pulishProductProjects = erpAutoPulishProductProjectMapper.selectList(
                    new LambdaQueryWrapper<ErpAutoPulishProductProject>()
                            .eq(ErpAutoPulishProductProject::getStoreId, storeId)
                            .like(projectName != null, ErpAutoPulishProductProject::getProjectName, projectName)
                            .eq(status != null, ErpAutoPulishProductProject::getStatus, status)
            );
            pulishProductProjects.forEach(project -> {
                HashMap<String, Object> map = new HashMap<>();
                map.put("id", project.getId().toString());
                map.put("name", project.getProjectName());
                map.put("storeId", project.getStoreId().toString());
                map.put("status", project.getStatus());
                map.put("pulishBeginDay", project.getPulishBeginDay());
                map.put("pulishEndDay", project.getPulishEndDay());
                map.put("type", 0);
                map.put("configData", project);
                mapList.add(map);
            });
        }
        if (ObjectUtils.isNotNull(myMap.get("1"))) {
            //下架列表
            List<ErpAutoUnshelfProductConfig> pulishProductProjects = erpAutoUnshelfProductConfigMapper.selectList(
                    new LambdaQueryWrapper<ErpAutoUnshelfProductConfig>()
                            .eq(ErpAutoUnshelfProductConfig::getStoreId, storeId)
                            .like(projectName != null, ErpAutoUnshelfProductConfig::getUnshelfName, projectName)
                            .eq(status != null, ErpAutoUnshelfProductConfig::getStatus, status)
            );
            pulishProductProjects.forEach(project -> {
                HashMap<String, Object> map = new HashMap<>();
                map.put("id", project.getId().toString());
                map.put("name", project.getUnshelfName());
                map.put("storeId", project.getStoreId().toString());
                map.put("status", project.getStatus());
                map.put("type", 1);
                map.put("configData", project);
                mapList.add(map);
            });
        }
        if (ObjectUtils.isNotNull(myMap.get("2"))) {
            //降价列表
            List<ErpAutoPriceOffProductProject> priceOffProductProjects = erpAutoPriceOffProductProjectMapper.selectList(
                    new LambdaQueryWrapper<ErpAutoPriceOffProductProject>()
                            .eq(ErpAutoPriceOffProductProject::getStoreId, storeId)
                            .like(projectName != null, ErpAutoPriceOffProductProject::getProjectName, projectName)
            );
            priceOffProductProjects.forEach(config -> {
                HashMap<String, Object> map = new HashMap<>();
                map.put("id", config.getId().toString());
                map.put("name", config.getProjectName());
                map.put("storeId", config.getStoreId().toString());
                map.put("status", config.getStatus());
                map.put("type", 2);
                map.put("configData", config);
                mapList.add(map);
            });
        }

        if (ObjectUtils.isNotNull(myMap.get("3"))) {
            //涨价列表
            List<ErpAutoPriceRiseProductProject> priceRiseProductProjects = erpAutoPriceRiseProductProjectMapper.selectList(
                    new LambdaQueryWrapper<ErpAutoPriceRiseProductProject>()
                            .eq(ErpAutoPriceRiseProductProject::getStoreId, storeId)
                            .like(projectName != null, ErpAutoPriceRiseProductProject::getProjectName, projectName)
            );
            priceRiseProductProjects.forEach(config -> {
                HashMap<String, Object> map = new HashMap<>();
                map.put("id", config.getId().toString());
                map.put("name", config.getProjectName());
                map.put("storeId", config.getStoreId().toString());
                map.put("status", config.getStatus());
                map.put("type", 3);
                map.put("configData", config);
                mapList.add(map);
            });
        }
        //查询测试
        if (ObjectUtils.isNotNull(myMap.get("4"))) {
            //自动化信息模块列表
            List<ErpAutoInfoModuleConfig> infoModuleConfigs = erpAutoInfoModuleConfigMapper.selectList(
                    new LambdaQueryWrapper<ErpAutoInfoModuleConfig>()
                            .eq(ErpAutoInfoModuleConfig::getStoreId, storeId)
                            .like(projectName != null, ErpAutoInfoModuleConfig::getModelName, projectName)
                            .eq(status != null, ErpAutoInfoModuleConfig::getStatus, status)
            );
            infoModuleConfigs.forEach(config -> {
                HashMap<String, Object> map = new HashMap<>();
                map.put("id", config.getId().toString());
                map.put("name", config.getModelName());
                map.put("storeId", config.getStoreId().toString());
                map.put("status", config.getStatus());
                map.put("type", 4);
                map.put("configData", config);
                mapList.add(map);
            });
        }

        // 整体测评规则
        if (ObjectUtils.isNotNull(myMap.get("5"))) {
            List<ErpAutoEvaluationConfig> evaluationConfigList
                    = erpAutoEvaluationConfigMapper.selectList(new LambdaQueryWrapper<ErpAutoEvaluationConfig>()
                    .eq(ErpAutoEvaluationConfig::getStoreId, storeId)
                    .like(projectName != null, ErpAutoEvaluationConfig::getEvaluationName, projectName));
            // 组合主规则数据
            evaluationConfigList.forEach(config -> {
                HashMap<String, Object> map = new HashMap<>();
                map.put("id", config.getId().toString());
                map.put("name", config.getEvaluationName());
                map.put("storeId", config.getStoreId().toString());
                map.put("status", config.getStatus());
                map.put("type", 5);
                map.put("configData", config);
                mapList.add(map);
            });
        }
        // 单品测评规则
        if (ObjectUtils.isNotNull(myMap.get("51"))){
            List<ErpAutoEvaluationConfigSingle> evaluationConfigSingleList
                    = erpAutoEvaluationConfigSingleMapper.selectList(new LambdaQueryWrapper<ErpAutoEvaluationConfigSingle>()
                    .eq(ErpAutoEvaluationConfigSingle::getStoreId, storeId)
                    .like(projectName != null, ErpAutoEvaluationConfigSingle::getEvaluationName, projectName));
            // 组合主规则数据
            evaluationConfigSingleList.forEach(config -> {
                HashMap<String, Object> map = new HashMap<>();
                map.put("id", config.getId().toString());
                map.put("name", config.getEvaluationName());
                map.put("storeId", config.getStoreId().toString());
                map.put("status", config.getStatus());
                map.put("type", 51);
                map.put("configData", config);
                mapList.add(map);
            });
        }
        // 计划测评规则
        if (ObjectUtils.isNotNull(myMap.get("52"))){
            List<ErpAutoEvaluationConfigTask> evaluationConfigTaskList
                    = erpAutoEvaluationConfigTaskMapper.selectList(new LambdaQueryWrapper<ErpAutoEvaluationConfigTask>()
                    .eq(ErpAutoEvaluationConfigTask::getStoreId, storeId)
                    .like(projectName != null, ErpAutoEvaluationConfigTask::getEvaluationName, projectName));
            // 组合主规则数据
            evaluationConfigTaskList.forEach(config -> {
                HashMap<String, Object> map = new HashMap<>();
                map.put("id", config.getId().toString());
                map.put("name", config.getEvaluationName());
                map.put("storeId", config.getStoreId().toString());
                map.put("status", config.getStatus());
                map.put("type", 52);
                map.put("configData", config);
                mapList.add(map);
            });
        }

        // 优惠券
        if (ObjectUtils.isNotNull(myMap.get("6"))) {
            LambdaQueryWrapper<ErpAutoSpeciesCouponConfig> queryWrapper = new LambdaQueryWrapper<ErpAutoSpeciesCouponConfig>()
                    .like(storeId != null, ErpAutoSpeciesCouponConfig::getStoreId, storeId);
            if (status != null) {
                if (status == 0) {
                    // 未生效，小于开始时间，大于结束时间
                    queryWrapper.lt(ErpAutoSpeciesCouponConfig::getStartTime, System.currentTimeMillis());
                    queryWrapper.gt(ErpAutoSpeciesCouponConfig::getEndTime, System.currentTimeMillis());
                }
                if (status == 1) {
                    // 生效中
                    queryWrapper.ge(ErpAutoSpeciesCouponConfig::getStartTime, System.currentTimeMillis());
                    queryWrapper.le(ErpAutoSpeciesCouponConfig::getEndTime, System.currentTimeMillis());
                }
                if (status == 2) {
                    // 失败
                    queryWrapper.eq(ErpAutoSpeciesCouponConfig::getStatus, status);
                }
            }
            List<ErpAutoSpeciesCouponConfig> configList = erpAutoSpeciesCouponConfigMapper.selectList(queryWrapper);
            configList.forEach(config -> {
                HashMap<String, Object> map = new HashMap<>();
                Long startTime = config.getStartTime();
                Long endTime = config.getEndTime();
                long now = System.currentTimeMillis();
                if (config.getStatus() == 1) {
                    if (now >= startTime && now <= endTime)
                        config.setStatus(1);
                    if (now < startTime || now > endTime)
                        config.setStatus(0);
                    else
                        config.setStatus(2);
                }
                map.put("id", config.getId().toString());
                map.put("name", config.getAeActivity() != null ?
                        config.getActivityName() + "(" + config.getAeActivity() + ")" : config.getActivityName());
                map.put("storeId", config.getStoreId().toString());
                map.put("status", config.getStatus());
                map.put("type", 6);
                map.put("configData", config);
                mapList.add(map);
            });
        }

        if (ObjectUtils.isNotNull(myMap.get("7"))) {
            LambdaQueryWrapper<ErpAutoGoldCoinConfig> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ErpAutoGoldCoinConfig::getStoreId, storeId);

            List<ErpAutoGoldCoinConfig> goldCoinConfigList = erpAutoGoldCoinConfigMapper.selectList(queryWrapper);
            goldCoinConfigList.forEach(config -> {
                HashMap<String, Object> map = new HashMap<>();
                map.put("id",config.getId().toString());
                map.put("name", "金币折扣");
                map.put("storeId", config.getStoreId().toString());
                map.put("status", 1);
                map.put("type", 7);
                map.put("configData", config);
                mapList.add(map);
            });

        }
        return R.ok(mapList);
    }
}
